package com.service.user;

import com.dao.BaseDao;
import com.dao.user.UserDao;
import com.dao.user.UserDaoImp;
import com.pojo.User;

import java.sql.Connection;
import java.sql.SQLException;

public class UserServiceImpl implements UserService{
    private UserDao userDao;
    public UserServiceImpl(){
        userDao = new UserDaoImp();
    }

    @Override
    public int apo(String Ac, String ke,String date) {
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            userDao.appointment(connection, Ac, ke,date);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return 1;
    }

    @Override
    public int res(String Ac, String Psw, String Name, String Sex, String Birth) {
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            userDao.setResUser(connection, Ac, Psw, Name, Sex, Birth);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return 1;
    }

    public User login(String Ac, String Psw){
        Connection connection = null;
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection,Ac);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        if (null != user) {
            if (!user.getPsw().equals(Psw)) {
                user = null;
            }
        }
        return user;
    }
}
